package com.channelsoft.cm.application;

import com.channelsoft.cm.application.tools.Tools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 启动界面
 */
public class MainActivity extends Activity {
	private PowerManager powerManager;
	private WakeLock wakeLock;
	private ImageView imageView;
	private boolean isNetworkAvialable = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.load);

		powerManager = (PowerManager) this
				.getSystemService(Context.POWER_SERVICE);
		wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "常亮");

		imageView = (ImageView) this.findViewById(R.id.loadImage);
		// 设置启动过场动画
		AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
		animation.setDuration(3000);
		imageView.setAnimation(animation);

		getSystemService(Context.CONNECTIVITY_SERVICE);

		animation.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				isNetworkAvialable = Tools
						.isNetworkAvialable(MainActivity.this);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// 检测网络是否可用, 如果不可用则跳转到wifi设置页面, 并退出本程序, 等待用户设置完毕再次启动
				if (!isNetworkAvialable) {
					Toast.makeText(MainActivity.this, "无法连接网络,请检查网络设置后重启本软件",
							Toast.LENGTH_LONG).show();
					MainActivity.this.startActivity(new Intent(
							Settings.ACTION_WIFI_SETTINGS));
					// 取消常亮
					// wakeLock.release();
					// 结束开始界面
					MainActivity.this.finish();
				} else {
					// 开启常亮
					wakeLock.acquire();
					// 等待实现-检查是否有已记录的用户名和密码, 并标记为自动登录, 如果有, 就使用这个用户名和密码开始登录,
					// 判断登录状况, 是否直接跳进业务界面或登录界面
					MainActivity.this.startActivity(new Intent(
							MainActivity.this, LoginActivity.class));
					MainActivity.this.finish();
				}
			}
		});

	}

}